package com.web22_2_1.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import java.util.Map;

public class JwtUtil {

    private static final String signKey = "web";//私钥
    private static final byte[] keyBytes = signKey.getBytes(); // 获取密钥的字节数组
    private static final Long expire = 3600* 1000L;//令牌有效期

    /**
     * 生成JWT令牌
     * @param claims JWT第二部分负载 payload 中存储的内容
     */
    public static String createJWT(Map<String, Object> claims){
        return Jwts.builder()
                .addClaims(claims)
                .signWith(SignatureAlgorithm.HS256, keyBytes)
                .setExpiration(new Date(System.currentTimeMillis() + expire))
                .compact();
    }

    public static Claims parseJWT(String jwt){
        return Jwts.parser()
                .setSigningKey(keyBytes)
                .parseClaimsJws(jwt)
                .getBody();
    }
}